package kassiopeia.commons.util;

import static org.fest.assertions.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;
import kassiopeia.commons.model.task.Connection;
import kassiopeia.commons.model.task.ParameterInstance;
import kassiopeia.commons.model.task.PluginInstance;
import kassiopeia.commons.model.task.TaskDescriptor;

public final class TaskModelUtils {

    public static final String TASK_NAME = "SampleTask";
    private static final String TASK_DESCRIPTION = "Sample Kassiopeia Task Definition";

    private TaskModelUtils() {}

    public static TaskDescriptor getTaskDescriptor() {
        return getTaskDescriptor(TASK_NAME);
    }

    public static TaskDescriptor getTaskDescriptor(String taskName) {
        TaskDescriptor task = new TaskDescriptor();

        task.setName(taskName);
        task.setDescription(TASK_DESCRIPTION);
        task.setPlugins(getPluginInstanceDescriptors());
        task.setConnections(getConnectionDescriptors());

        return task;
    }

    public static void removePluginFromTask(TaskDescriptor descriptor, String instanceId) {
        for(PluginInstance instance : descriptor.getPlugins()) {
            if(instance.getInstanceId().equals(instanceId)) {
                descriptor.getPlugins().remove(instance);
                break;
            }
        }
    }

    public static void assertEquals(TaskDescriptor expected, TaskDescriptor actual) {
        assertThat(actual).isNotNull();
        assertThat(actual.getName()).isEqualTo(expected.getName());
        assertThat(actual.getDescription()).isEqualTo(expected.getDescription());
        assertThat(actual.getPlugins()).hasSize(expected.getPlugins().size());
        assertThat(actual.getConnections()).hasSize(expected.getConnections().size());
    }

    private static List<PluginInstance> getPluginInstanceDescriptors() {
        PluginInstance p1a = new PluginInstance("p1a", "p1", "test", "1.0");
        PluginInstance p1b = new PluginInstance("p1b", "p1", "test", "1.0");
        PluginInstance p2  = new PluginInstance("p2", "p2", "test", "1.0");
        PluginInstance p3  = new PluginInstance("p3", "p3", "test", "1.0");

        ParameterInstance pd1 = new ParameterInstance("parameter1", "123");
        ParameterInstance pd2 = new ParameterInstance("parameter2", "some text");
        p2.setConfiguration(Arrays.asList(pd1, pd2));

        return Arrays.asList(new PluginInstance[]{p1a, p1b, p2, p3});
    }

    private static List<Connection> getConnectionDescriptors() {
        Connection c1 = new Connection("p1a", "A", "p3");
        Connection c2 = new Connection("p1a", "B", "p2");
        Connection c3 = new Connection("p1a", "B", "p4");
        Connection c4 = new Connection("p3",  "B", "p1b");
        Connection c5 = new Connection("p2",  "A", "p1b");
        Connection c6 = new Connection("p3",  "A", "p4");
        Connection c7 = new Connection("p1b", "A", "p4");

        return Arrays.asList(new Connection[]{c1, c2, c3, c4, c5, c6, c7});
    }
}
